package org.gp953.product.service.impl;

import org.gp953.basic.service.impl.BaseServiceImpl;
import org.gp953.product.domain.Product;
import org.gp953.product.domain.ProductDetail;
import org.gp953.product.mapper.ProductDetailMapper;
import org.gp953.product.mapper.ProductMapper;
import org.gp953.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void add(Product product) {
        //保存服务信息
        productMapper.save(product);
        //保存服务详情
        ProductDetail detail = product.getDetail();
        if (detail != null) {
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void delete(Long id) {
        if (id !=null) {
            productDetailMapper.deleteByProductId(id);
        }
        productMapper.delete(id);

    }


    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime", new Date());
        productMapper.onSale(map);

    }


    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime", new Date());
        productMapper.offSale(map);

    }
}
